package com.gridmobi.xrtb.model.rtb;


import com.gridmobi.xrtb.handler.impl.BidHandler;
import com.gridmobi.xrtb.model.openrtb23.Bid;
import com.gridmobi.xrtb.model.openrtb23.Imp;
import com.gridmobi.xrtb.util.DOMUtils;
import com.gridmobi.xrtb.util.RtbUtil;
import org.apache.commons.lang3.StringUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

/**
 *
 */

public class VastTemplate {

    public static boolean exists(int[] apis, int api) {
        boolean r = false;
        for (int i : apis) {
            if (i == api) {
                r = true;
            }
        }
        return r;

    }


    /**
     * 一小时的秒数
     */
    private static final int HOUR_SECOND = 60 * 60;

    /**
     * 一分钟的秒数
     */
    private static final int MINUTE_SECOND = 60;

    /**
     * 根据秒数获取时间串
     *
     * @param second (eg: 100s)
     * @return (eg : 00 : 01 : 40)
     */
    public static String getTimeStrBySecond(int second) {
        if (second <= 0) {

            return "00:00:00";
        }

        StringBuilder sb = new StringBuilder();
        int hours = second / HOUR_SECOND;
        if (hours > 0) {

            second -= hours * HOUR_SECOND;
        }

        int minutes = second / MINUTE_SECOND;
        if (minutes > 0) {

            second -= minutes * MINUTE_SECOND;
        }

        return (hours >= 10 ? (hours + "")
                : ("0" + hours) + ":" + (minutes >= 10 ? (minutes + "") : ("0" + minutes)) + ":"
                + (second >= 10 ? (second + "") : ("0" + second)));
    }

    public static String vast2NonLinearTpl(Campaign winCampaign, String impUrl, String clickUrl, Imp imp) {

        return "";
    }

    public static String vast2LinearTpl(Bid bid, Campaign winCampaign, String impUrl, String clickUrl, Imp imp, String impUrl2) {

         /*
         linear &&  vpaid
         */
        Document document = DOMUtils.newXMLDocument();
        Element root = document.createElement("VAST");
        document.appendChild(root);
        root.setAttribute("version", "2.0");
        Element ad = document.createElement("Ad");
        ad.setAttribute("id", bid.getAdid());
        root.appendChild(ad);

        Element inline = document.createElement("InLine");
        ad.appendChild(inline);

        Element adSystem = document.createElement("AdSystem");
        adSystem.setTextContent(BidHandler.BID_SEAT);
        inline.appendChild(adSystem);

        Element adTitle = document.createElement("AdTitle");
        adTitle.setTextContent(imp.getId());
        inline.appendChild(adTitle);

        Element impression = document.createElement("Impression");
        impression.setAttribute("id", imp.getId());
        impression.appendChild(document.createCDATASection(StringUtils.isBlank(impUrl2)?impUrl:impUrl2));
        inline.appendChild(impression);

        Element creatives = document.createElement("Creatives");
        inline.appendChild(creatives);
        Element creative = document.createElement("Creative");
        creatives.appendChild(creative);
        Element linear = document.createElement("Linear");
        creative.appendChild(linear);
        Element duration = document.createElement("Duration");
        duration.setTextContent(getTimeStrBySecond(winCampaign.getCreative().getVideoduration()));
        linear.appendChild(duration);

        Element trackingEvents = document.createElement("TrackingEvents");

        Element track1 = document.createElement("Tracking");
        track1.setAttribute("event", "start");
        track1.appendChild(document.createCDATASection(impUrl.concat("&event=start")));
        Element track2 = document.createElement("Tracking");
        track2.setAttribute("event", "firstQuartile");
        track2.appendChild(document.createCDATASection(impUrl.concat("&event=firstQuartile")));
        Element track3 = document.createElement("Tracking");
        track3.setAttribute("event", "thirdQuartile");
        track3.appendChild(document.createCDATASection(impUrl.concat("&event=thirdQuartile")));
        Element track4 = document.createElement("Tracking");
        track4.setAttribute("event", "complete");
        track4.appendChild(document.createCDATASection(impUrl.concat("&event=complete")));

        trackingEvents.appendChild(track1);
        trackingEvents.appendChild(track2);
        trackingEvents.appendChild(track3);
        trackingEvents.appendChild(track4);
        linear.appendChild(trackingEvents);


        Element videoClicks = document.createElement("VideoClicks");
        Element clickThrough = document.createElement("ClickThrough");
        clickThrough.setAttribute("id", imp.getId());
        clickThrough.appendChild(document.createCDATASection(clickUrl));
        videoClicks.appendChild(clickThrough);
        linear.appendChild(videoClicks);
        Element mediaFiles = document.createElement("MediaFiles");
        Element mediaFile = document.createElement("MediaFile");
        mediaFile.setAttribute("height", winCampaign.getCreative().getH().toString());
        mediaFile.setAttribute("width", winCampaign.getCreative().getW().toString());
        if (StringUtils.isBlank(winCampaign.getCreative().getMime())) {
            mediaFile.setAttribute("type", "video/mp4");

        } else {
            mediaFile.setAttribute("type", winCampaign.getCreative().getMime());
        }
        mediaFile.setAttribute("delivery", "progressive");
        mediaFile.setAttribute("bitrate", winCampaign.getCreative().getBsize().toString());
        mediaFile.appendChild(document.createCDATASection(RtbUtil.packCreativeUrl(imp.getSecure(),winCampaign.getCreative().getUrl())));
        mediaFiles.appendChild(mediaFile);
        linear.appendChild(mediaFiles);
        String xmlStr = DOMUtils.toStringFromDoc(document);
        return xmlStr.trim();

    }

    public static String vast2Native( Bid bid ,Imp imp, String impUrl, String clickUrl
            ,int videoduration,int h ,int w ,String mime ,String mediaurl ) {

         /*
         linear &&  vpaid
         */
        Document document = DOMUtils.newXMLDocument();
        Element root = document.createElement("VAST");
        document.appendChild(root);
        root.setAttribute("version", "2.0");
        Element ad = document.createElement("Ad");
        ad.setAttribute("id", bid.getAdid());
        root.appendChild(ad);

        Element inline = document.createElement("InLine");
        ad.appendChild(inline);

        Element adSystem = document.createElement("AdSystem");
        adSystem.setTextContent(BidHandler.BID_SEAT);
        inline.appendChild(adSystem);

        Element adTitle = document.createElement("AdTitle");
        adTitle.setTextContent(imp.getId());
        inline.appendChild(adTitle);

        Element impression = document.createElement("Impression");
        impression.setAttribute("id", imp.getId());
        impression.appendChild(document.createCDATASection(impUrl));
        inline.appendChild(impression);

        Element creatives = document.createElement("Creatives");
        inline.appendChild(creatives);
        Element creative = document.createElement("Creative");
        creatives.appendChild(creative);
        Element linear = document.createElement("Linear");
        creative.appendChild(linear);
        Element duration = document.createElement("Duration");
        duration.setTextContent(getTimeStrBySecond(videoduration));
        linear.appendChild(duration);

        Element trackingEvents = document.createElement("TrackingEvents");

        Element track1 = document.createElement("Tracking");
        track1.setAttribute("event", "start");
        track1.appendChild(document.createCDATASection(impUrl.concat("&event=start")));
        Element track2 = document.createElement("Tracking");
        track2.setAttribute("event", "firstQuartile");
        track2.appendChild(document.createCDATASection(impUrl.concat("&event=firstQuartile")));
        Element track3 = document.createElement("Tracking");
        track3.setAttribute("event", "thirdQuartile");
        track3.appendChild(document.createCDATASection(impUrl.concat("&event=thirdQuartile")));
        Element track4 = document.createElement("Tracking");
        track4.setAttribute("event", "complete");
        track4.appendChild(document.createCDATASection(impUrl.concat("&event=complete")));

        trackingEvents.appendChild(track1);
        trackingEvents.appendChild(track2);
        trackingEvents.appendChild(track3);
        trackingEvents.appendChild(track4);
        linear.appendChild(trackingEvents);


        Element videoClicks = document.createElement("VideoClicks");
        Element clickThrough = document.createElement("ClickThrough");
        clickThrough.setAttribute("id", imp.getId());
        clickThrough.appendChild(document.createCDATASection(clickUrl));
        videoClicks.appendChild(clickThrough);
        linear.appendChild(videoClicks);
        Element mediaFiles = document.createElement("MediaFiles");
        Element mediaFile = document.createElement("MediaFile");
        mediaFile.setAttribute("height", String.valueOf(h));
        mediaFile.setAttribute("width",String.valueOf(w));
        if (StringUtils.isBlank(mime)) {
            mediaFile.setAttribute("type", "video/mp4");

        } else {
            mediaFile.setAttribute("type", mime);
        }
        mediaFile.setAttribute("delivery", "progressive");
        mediaFile.appendChild(document.createCDATASection(RtbUtil.packCreativeUrl(imp.getSecure(),mediaurl)));
        mediaFiles.appendChild(mediaFile);
        linear.appendChild(mediaFiles);
        String xmlStr = DOMUtils.toStringFromDoc(document);
        return xmlStr.trim();

    }

    public static void main(String[] args) {
        System.out.println(getTimeStrBySecond(100));
    }

}

